var apps, views, merchant, first = 0, last = 10;
mui.plusReady(function () {
   views = bexta.getWebview();
   merchant = bexta.getStorage(MERCHANT);
   var d = bexta.getDate([new Date().getTime()]);
   d = d[0].allDate.split('-');
   var df = d[0] + '-' + d[1];
   bexta.ajax(api.account_order, function(res){
   		initVue(res, df);
   }, {data:{uid:merchant.uid, firstrow:first, listrow:last,type:0, time:df}});
});
function initVue(res, df){
	apps = new Vue({
		el:"#vueMain",
		data:{
			list:res,
			first:res.length,
			last:last,
			stype:"0",
			sdate:df
		},
		mounted:function(){ 
			var self = this;
			initEvent();
			document.querySelector('.loading').classList.add('loadRun');
			mui('.list-scroll').scroll();
			mui.init({
				  	pullRefresh : {
				    		container:"#refreshContainer",
					    up : {
					      	height:50,
					      	auto:false,
					      	contentrefresh : "正在加载...",
					      	contentnomore:'没有更多数据了',
					      	callback :function(){
					      		var that = this;
					      		bexta.ajax(api.account_order, function(res){
					      			var len = res.length;
					      			if( len == 0 || len < self.last ){
					      				that.endPullupToRefresh(true);
					      			}else{
					      				that.endPullupToRefresh(false);
					      			}
					      			if( len > 0 ){
					      				mui.each(res, function(){
					      					self.list.push(this);
					      				});
					      				self.first += len;
					      			}
					      		}, {data:{uid:merchant.uid, firstrow:self.first, listrow:self.last, type:self.stype, time:self.sdate}})
					      	}
					    }
				  	}
				});
		},
		updated:function(){
			mui('#refreshContainer').pullRefresh().refresh(true);
   			mui('#refreshContainer').pullRefresh().scrollTo(0,0,100)
		}
	});
}
function initEvent(){
	var windowHeight = parseInt(window.innerHeight*0.6); //最大高度
   	mui('.select-list').each(function(index, ele){
   		//要先计算出里面mui-scroll的高度
   		var boxHeight = this.querySelector('.mui-scroll').clientHeight;
   		var transformHeight = 0; //运作 动画的高度
   		//判断是否大于最大高度
   		if( boxHeight > windowHeight ){
   			//大于,那么就用最大的高度作为高度
   			transformHeight = windowHeight;
   			this.style.height = (windowHeight+50) +'px';
   		}else{
   			//小于最大高度,那么直接等于这于这个高度
   			transformHeight = boxHeight;
   			this.style.height = (boxHeight + 50) + 'px';
   		}
   		this.style.top = - transformHeight + 'px';
   	}); 
   	var dtPicker = new mui.DtPicker({ 
   		type:'month'
   	});
   	mui('body').on('tap', '.dselcd', function(evt){
   		evt.stopPropagation()
   		var tab = this.getAttribute('data-tab');
   		if( !tab ){
   			return ;
   		} 
// 		dtPicker.hide()
   		var tabobj = document.querySelector('#'+tab);
   		if( tabobj.classList.contains('run') ){
   				this.classList.remove('run');
   				tabobj.classList.remove('run');
   				document.querySelector('.mark').style.display = 'none';
   			}else{
   				mui('.dselcd').each(function(){
   					this.classList.remove('run');
   				});
   				//要清除所有运行中的动画
   				mui('.select-list').each(function(){
   					this.classList.remove('run');
   				});
   				this.classList.add('run');
   				document.querySelector('.mark').style.display = 'block';
   				mui.later(function(){
   					this.classList.add('run');
   				},10, tabobj )
   				
   			}
   	});
   	document.querySelector('.mark').addEventListener('tap', function(evt){
   		//点击模态背景 删除所有动画
   		evt.stopPropagation()
   		document.querySelector('.select-list.run').classList.remove('run');
   		document.querySelector('.dselcd.run').classList.remove('run');
   		this.style.display = 'none';
   	});
   	
   	//重置一下checkbox, 解决事件触发两次的bug
   	mui('.sel-list').on('tap', '.mui-checkbox', function(evt){
   		var input = this.querySelector('input');
   		mui.each(document.querySelectorAll('.sel-list input'), function(){
   			this.checked = false;
   		});
   		mui.later(function(){
   			input.checked = true;	
   		},10);
   	});
   	//点击确定按钮
   	mui('.select-btn').on('tap', '.select-btn-submit', function(){
   		mui.each(document.querySelectorAll('.sel-list input'), function(){
   			if(this.checked){
   				apps.stype = this.value;
   			}
   		});
   		document.querySelector('.mark').style.display = 'none';
   		mui('.dselcd').each(function(){
			this.classList.remove('run');
		});
		//要清除所有运行中的动画
		mui('.select-list').each(function(){
			this.classList.remove('run');
		});
   		bexta.ajax(api.account_order, function(res){
   			apps.list = res;
   			apps.first = res.length;
   			mui('#refreshContainer').pullRefresh().refresh(true);
   			mui('#refreshContainer').pullRefresh().scrollTo(0,0,100)
   		}, {data:{uid:merchant.uid, firstrow:0, listrow:apps.last, type:apps.stype, time:apps.sdate}});
   	});
   	//重置按钮
   mui('.select-btn').on('tap', '.select-btn-reset', function(){
   		document.querySelector('.mark').style.display = 'none';
   		mui('.dselcd').each(function(){
			this.classList.remove('run');
		});
		//要清除所有运行中的动画
		mui('.select-list').each(function(){
			this.classList.remove('run');
		});
		bexta.ajax(api.account_order, function(res){
   			apps.list = res;
   			apps.first = res.length;
   		}, {data:{uid:merchant.uid, firstrow:0, listrow:apps.last, type:apps.stype, time:apps.sdate}});
   });
   	//日期选择
   	document.querySelector('#date').addEventListener('tap', function(){
   		document.querySelector('.mark').style.display = 'none'; 
   		mui('.dselcd').each(function(){
			this.classList.remove('run');
		});
		//要清除所有运行中的动画
		mui('.select-list').each(function(){
			this.classList.remove('run');
		});
   		dtPicker.show(function(item){
   			apps.sdate = item.value ;
   			bexta.ajax(api.account_order, function(res){
	   			apps.list = res;
	   			apps.first = res.length;
	   		}, {data:{uid:merchant.uid, firstrow:0, listrow:apps.last, type:apps.stype, time:apps.sdate}});
   		});
   	});
   	document.querySelector('.mark').addEventListener('tap', function(){
   		//点击模态背景 删除所有动画
   		mui('.select-list').each(function(){  
   					this.classList.remove('run');
   				});
   		mui('.dselcd').each(function(){
   					this.classList.remove('run');
   				});
   		this.style.display = 'none';
   	});
}
	